import Vue from "vue";
import VueRouter from "vue-router";
import HomeView from "../views/HomeView.vue";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/home",
    name: "home",
    component: HomeView,
  },
  {
    path: "/about",
    name: "about",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/AboutView.vue"),
  },
];

const router = new VueRouter({
  mode: "history",
  base: window.__POWERED_BY_QIANKUN__ ? "/sub-app2" : "/",
  routes: window.__POWERED_BY_QIANKUN__ ? [] : routes,
});

router.beforeEach((to, from, next) => {
  const allRouter = router.getRoutes();
  if (allRouter.findIndex((i) => i.path === to.path) === -1) {
    window.history.replaceState({}, false, "/404");
    return;
  }
  next();
});

export default router;
